/**
 * @author Aaron
 * @createTime 14:54 2022/5/16
 **/
package services

import (
	"fmt"
	"longmen/server/pkg/common/helper/util"
	"longmen/server/pkg/db/redis/cache"
	"sort"
)

/**
 * @author Aaron
 * @description 计算主播等级
 * @createTime 14:54 2022/5/16
 **/
func (s *Service) CalculateAnchorLevel(point uint32) (uint8, error) {
	var level uint8
	fName := "CalculateUserLevel"
	rules, err := cache.GetAnchorLevelRule()
	if err != nil {
		return 0, fmt.Errorf("%s%s", util.Convert(fName), err.Error())
	}
	sort.Slice(rules, func(i, j int) bool {
		return rules[i].Level < rules[j].Level
	})
	for i := 0; i < len(rules); i++ {
		if point >= rules[i].Point {
			level = rules[i].Level
			break
		}
	}
	return level, nil
}
